﻿using System;
using System.Collections.Generic;

namespace T.FAS.Runtime.Base.Entity
{
    /// <summary>
    /// 行为结果集合结构
    /// </summary>
    public class ViewModelSchema
    {
        public ViewModelSchema()
        {
        }
        public ViewModelSchema(string mainvmItemCode) 
        {
            MainViewModelItemCode = mainvmItemCode;
        }
        public List<ViewModelItem> Current { get; set; }
        /// <summary>
        /// Current在List中的序号，从0~Length-1
        /// </summary>
        public int CurrentRowNumber { get; set; }


        private string _mainViewModelItemCode = string.Empty;
        public string MainViewModelItemCode
        {
            get
            {
                if (string.IsNullOrEmpty(_mainViewModelItemCode) && Current != null && Current.Count > 0)
                {
                    _mainViewModelItemCode = Current[0].ViewModelItemCode;
                }
                return _mainViewModelItemCode;
            }
            set { _mainViewModelItemCode = value; }
        }
        public ViewModelItem MainViewModelItem
        {
            get
            {
                if (Current != null && Current.Count > 0)
                {
                    return Current.Find(x => x.ViewModelItemCode.Equals(MainViewModelItemCode));
                }
                return new ViewModelItem();
            }
        }
        public int Length { get; set; }
        public bool Empty { get; set; }
        
        public void AddProperty(ViewModelItemProperty property)
        {
            if (Current != null && Current.Count > 0)
            {
                Current[0].AddProperty(property);
            }
        }
        public void AddProperty(string vmItemName, ViewModelItemProperty property)
        {
            var vmi = Current.Find(v=>v.ViewModelItemName.Equals(vmItemName,StringComparison.OrdinalIgnoreCase));
            if (vmi != null)
                vmi.AddProperty(property);
        }
    }
}
